package com.henu.engineer.controller.teacher

import com.alibaba.fastjson.JSONObject
import com.henu.engineer.StringUtils
import com.henu.engineer.service.FileService
import com.henu.engineer.service.teacher.StudentCourseScoreService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile

/**
 * 查看所在课程组的课程的所有学生的成绩，包括该门课程的各个考核方式的成绩，综合成绩。可以编辑个别成绩信息。
 * @author noexcept
 * @since 2021/8/12 22:15
 */
@RestController
@RequestMapping("/tc")
class StudentCourseScoreController {

    @Autowired
    lateinit var studentCourseScoreService: StudentCourseScoreService

    @Autowired
    lateinit var fileService: FileService

    /**
     * 返回所有学生某门课程的综合成绩
     */
    @GetMapping("/allComprehensiveScore")
    fun courseComprehensiveScore(courseId: String?): String? {
        val res = JSONObject()
        if (StringUtils.isEmpty(courseId)) {
            res["code"] = 1
            res["msg"] = "PARAM_INCOMPLETE"
            return res.toJSONString()
        }
        res["code"] = 0
        res["msg"] = "success"
        res["data"] = studentCourseScoreService.getCourseComprehensiveScoreOfAllStudent(courseId!!)
        return res.toJSONString()
    }

    /**
     * 某课程所有学生的各考核方式成绩
     */
    @GetMapping("/courseMethodScoreOfAllStudent")
    fun courseMethodScoreOfAllStudent(courseId: String?): String? {
        val res = JSONObject()
        if (StringUtils.isEmpty(courseId)) {
            res["code"] = 1
            res["msg"] = "PARAM_INCOMPLETE"
            return res.toJSONString()
        }
        res["code"] = 0
        res["msg"] = "success"
        res["data"] = studentCourseScoreService.getCourseMethodScoreOfAllStudent(courseId!!)
        return res.toJSONString()
        /*
                    测试结果示例
                  {
                    "courseId": "00450246",
                    "courseName": "C#",
                    "data": [
                    {
                        "id": "1925060282",
                        "name": "李金宝",
                        "classNum": "03",
                        "semester": "4",
                        "data": [
                        {
                            "method": "期末考试",
                            "score": 96
                        },
                        {
                            "method": "平时成绩",
                            "score": 85
                        }
                        ]
                    },
                    {
                        "id": "1925060283",
                        "name": "张三",
                        "classNum": "03",
                        "semester": "4",
                        "data": [
                        {
                            "method": "期末考试",
                            "score": 100
                        },
                        {
                            "method": "平时成绩",
                                "score": 100
                        }
                        ]
                    }
                    ]
                }
        */
    }

    /**
     * 修改某个学生的某个课程的某个考核方式成绩
     */
    @PatchMapping("/courseMethodScoreOfAStudent")
    fun courseMethodScoreOfAStudent(courseId: String?, studentId: String?, method: String?, score: Double?): String? {
        val res = JSONObject()
        if (StringUtils.isEmpty(courseId, studentId, method)) {
            res["code"] = 1
            res["msg"] = "PARAM_INCOMPLETE"
            return res.toJSONString()
        }
        res["msg"] = "success"
        if (studentCourseScoreService.updateCourseMethodScoreOfAStudent(courseId!!, studentId!!, method!!, score!!)) {
            res["code"] = 0
            res["msg"] = "SUCCESS"
        } else {
            res["code"] = 2
            res["msg"] = "FAILED"
        }
        return res.toJSONString()
    }

    /**
     * 上传学生成绩表
     */
    @PostMapping("/studentScoreDtl")
    fun studentScoreDtl(fileUpload: MultipartFile?, courseId: String?, testPaperId: String?): String {
        val res = JSONObject()
        if (fileUpload == null || StringUtils.isEmpty(courseId, testPaperId)) {
            res["code"] = 1
            res["msg"] = "parameter not enough"
            return res.toJSONString()
        }
        val pair = fileService.saveUploadedFile(fileUpload)
        if (pair.second == null) {
            return pair.first
        }
        res["code"] = 0
        res["msg"] = fileService.parseUploadedExcelFileOfStudentScoreDtl(pair.second!!, courseId!!, testPaperId!!)
        return res.toJSONString()
    }
}